﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using WarehouseManagement.BusinessLogic.BC;

namespace WarehouseManagement.Views.Currency
{
    public partial class NewEditCurrency : DevExpress.XtraEditors.XtraForm
    {
        private bool _isEditMode;
        private CurrencyBC _currencyBC;

        public delegate void OperationCompleted(bool success, bool isEditMode);
        public OperationCompleted OnOperationCompleted;

        public NewEditCurrency(bool isEditMode = false)
        {
            InitializeComponent();
            _isEditMode = isEditMode;
            _currencyBC = new CurrencyBC();
            if (isEditMode)
            {
                this.Text = "Sửa đơn vị tiền tệ";

            }
            else
            {
                this.Text = "Thêm đơn vị tiền tệ";
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            var errList = ValidateCategory(_isEditMode);
            if (errList.Count() > 0)
            {
                XtraMessageBox.Show(string.Join("\n", errList), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            try
            {
                var dto = new BusinessLogic.DTO.CurrencyDTO()
                    {
                        CurrencyId = this.Id,
                        CurrencyCode = this.CurrencyCode,
                        CurrencyName = this.CurrencyName,
                        ExchangeRateVND = this.ExchangeRate
                    };
                if (!_isEditMode)
                {
                    _currencyBC.AddCurrency(dto);
                }
                else
                {
                    _currencyBC.UpdateCurrency(dto);
                }
                FireOperationCompleted(true, _isEditMode);
                this.Close();
            }
            catch (Exception)
            {
                FireOperationCompleted(false, _isEditMode);
            }
        }
        private void FireOperationCompleted(bool success, bool isEditMode)
        {
            if (OnOperationCompleted != null)
                OnOperationCompleted(success, isEditMode);
        }
        public string CurrencyCode
        {
            get { return txtCurrencyCode.Text; }
            set { txtCurrencyCode.Text = value; }
        }
        public string CurrencyName
        {
            get { return txtCurrencyName.Text; }
            set { txtCurrencyName.Text = value; }
        }
        public decimal ExchangeRate
        {
            get { return txtExchangeRate.Value; }
            set { txtExchangeRate.Value = value; }
        }

        public int Id { get; set; }

        private IEnumerable<string> ValidateCategory(bool editMode)
        {
            if (string.IsNullOrEmpty(CurrencyCode))
                yield return "Yêu cầu nhập mã";
            if (string.IsNullOrEmpty(CurrencyName))
                yield return "Yêu cầu nhập tên";

            if (!editMode)
            {
                if (_currencyBC.GetCurrencyByCode(this.CurrencyCode.Trim()) != null)
                {
                    yield return "Yêu cầu nhập mã khác";
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}